<?php
            


/**
 * Desc:
 * Time: 2021/6/22 7:47 下午
 */
require_once PATH_MODULE .'define.php';
class ShopplanModel extends PluginModel {

    /*以下店铺配置*/
    //插入店铺配置
    public static function insertConfig($data)
    {
        $res = $insertid = db_insert(plan_shop_config,$data);
        if (!empty($res)) {
            return $insertid;
        } else {
            return false;
        }
    }

    //修改店铺配置
    public static function editConfigByWhere($where,$data)
    {

        $res = db_update(plan_shop_config,$data,$where);
        if (!empty($res)) {
            return true;
        } else {
            return false;
        }
    }

    //从流水id获取店铺配置
    public static function getConfigById($id)
    {
        $res = db_get(plan_shop_config,['id'=>$id]);
        return $res;
    }
    //从店铺id获取店铺配置
    public static function getConfigByStoreId($storeid)
    {
        $res = db_get(plan_shop_config,['storeid'=>$storeid]);
        return $res;
    }

    /*以上店铺配置*/

    /*以下分店时间配置*/
    //插入分店时间配置
    public static function insertTimeConfig($data)
    {
        $res = $insertid = db_insert(plan_shoptime_config,$data);
        if (!empty($res)) {
            return $insertid;
        } else {
            return false;
        }
    }
    //修改分店时间配置
    public static function editTimeConfigByWhere($where,$data)
    {

        $res = db_update(plan_shoptime_config,$data,$where);
        if (!empty($res)) {
            return true;
        } else {
            return false;
        }
    }
    //删除指定店铺id+分店shopid的配置
    public static function delshopconfig($storeid,$shopid)
    {
         return db_delete(plan_shoptime_config,['storeid'=>$storeid,'shopid'=>$shopid]);
    }


    //从流水id获取分店时间配置
    public static function getTimeConfigById($id)
    {
        $res = db_get(plan_shoptime_config,['id'=>$id]);
        return $res;
    }
    //从店铺id+店铺id，获取分店时间配置
    public static function getTimeConfigByStoreId($storeid,$shopid)
    {
        $res = db_get(plan_shoptime_config,['storeid'=>$storeid,'shopid'=>$shopid]);
        return $res;
    }
    /*以上分店时间配置*/


    /*以下分店规划*/
    //插入分店特殊时间规划
    public static function insertSpecialWorktimes($data)
    {
        $res = $insertid = db_insert(plan_special_worktimes,$data);
        if (!empty($res)) {
            return $insertid;
        } else {
            return false;
        }
    }
    //修改分店特殊时间规划
    public static function editSpecialWorktimesByWhere($where,$data)
    {

        $res = db_update(plan_special_worktimes,$data,$where);
        if (!empty($res)) {
            return true;
        } else {
            return false;
        }
    }

    //从流水id获取分店特殊时间规划
    public static function getSpecialWorktimesById($id)
    {
        $res = db_get(plan_special_worktimes,['id'=>$id]);
        return $res;
    }


    //获取相同的plantimeid规划列表
    public static function getSpecialWorktimesList($plantimeid)
    {
        $sql = " SELECT * FROM ".tablename(plan_special_worktimes)." WHERE plantimeid=$plantimeid";
        return db_select($sql);
    }
    //获取相同storeid的规划列表（没有禁用的）
    public static function getSpecialWorktimesListByStoreid($storeid)
    {
        $sql = " SELECT * FROM ".tablename(plan_special_worktimes)." WHERE storeid=$storeid AND status=1";
        return db_select($sql);
    }
    //获取相同storeid的规划列表（包括禁用的）
    public static function getAllSpecialWorktimesList($storeid)
    {
        $sql = " SELECT * FROM ".tablename(plan_special_worktimes)." WHERE storeid=$storeid";
        return db_select($sql);
    }

    public static function getDefaultSpecialWorkTimeItem($storeid,$tplname)
    {
        $res = db_get(plan_special_worktimes,['storeid'=>$storeid,'tplname'=>$tplname]);
        return $res;
    }

    public static function getSpecialWorkTimesListTotal($storeid)
    {
        $sql = " select count(*) as cnt from ".tablename(plan_special_worktimes)." where storeid=$storeid";
        return db_find($sql)['cnt'];
    }
    //删除该id规划
    public static function delSpecial($id)
    {
         return db_delete(plan_special_worktimes,['id'=>$id]);
    }
    //删除该planTimeid的所有规划
    public static function delSpecials($plantimeid)
    {
         return db_delete(plan_special_worktimes,['plantimeid'=>$plantimeid]);
    }
    /*以上分店规划*/


    /*以下客户预约表*/
    //插入客户预约
    public static function insertUserPlan($data)
    {
        $res = $insertid = db_insert(plan_user_planning,$data);
        if (!empty($res)) {
            return $insertid;
        } else {
            return false;
        }
    }

    //修改客户预约
    public static function editUserPlanByWhere($where,$data)
    {

        $res = db_update(plan_user_planning,$data,$where);
        if (!empty($res)) {
            return true;
        } else {
            return false;
        }
    }

    //从流水id获取客户预约
    public static function getUserPlanById($id)
    {
        $res = db_get(plan_user_planning,['id'=>$id]);
        return $res;
    }
    //从用户uid获取客户预约
    public static function getUserPlanByUid($uid,$themeid)
    {
        $res = db_get(plan_user_planning,['uid'=>$uid,'storeid'=>$themeid]);
        return $res;
    }

    //从店铺id+分店id获取客户预约总量
    public static function getUserPlanByStoreId($storeid,$shopid)
    {
        $sql = " SELECT * FROM ".tablename(plan_user_planning)." WHERE storeid=$storeid AND shopid=$shopid ORDER BY created_at DESC";
        $res = db_select($sql);
        return $res;
    }

    //搜索获取group by时间段的“客户已预约”数量
    public static function searchUserPlan($storeid,$shopid,$question)
    {
        $condition="";
        if (!empty($question)) {
            $condition .= " AND name LIKE '%".$question."%'"." OR title like '%".$question."%'";
        }
        $condition .= " GROUP BY DATE_FORMAT(plan_at,'%Y-%m-%d') ORDER BY created_at DESC  ";

        $sql = " SELECT DATE_FORMAT(plan_at, '%Y-%m-%d' ) AS p_date, COUNT(*) AS nums FROM ".tablename(plan_user_planning)." WHERE storeid=$storeid AND shopid=$shopid".$condition;
        return db_select($sql);
    }
    //搜索获取指定时间段的group by的“客户已预约”数量(按天group by)
    public static function searchUserPlaned($storeid,$shopid,$time)
    {
        $condition="";
        $condition .= " GROUP BY DATE_FORMAT(plan_at,'%Y-%m-%d') ORDER BY created_at DESC  ";

        $sql = " SELECT DATE_FORMAT(plan_at, '%Y-%m-%d' ) AS p_date, COUNT(*) AS nums FROM ".tablename(plan_user_planning)." WHERE storeid=$storeid AND shopid=$shopid AND DATE_FORMAT(plan_at,'%Y-%m-%d')=DATE_FORMAT('$time', '%Y-%m-%d')".$condition;
        return db_select($sql);
    }

    //搜索获取指定时间段的group by的“客户已预约”数量(按时间段group by)
    public static function searchUserPlaned4($storeid,$shopid,$time)
    {
        $condition="";
        $condition .= " GROUP BY plan_at ORDER BY created_at DESC  ";

        $sql = " SELECT DATE_FORMAT(plan_at, '%Y-%m-%d %H:%i' ) AS p_date, COUNT(*) AS nums FROM ".tablename(plan_user_planning)." WHERE storeid=$storeid AND shopid=$shopid AND DATE_FORMAT(plan_at,'%Y-%m-%d')=DATE_FORMAT('$time', '%Y-%m-%d')".$condition;
        return db_select($sql);
    }
    //获取所有storeid(themeid)的预约
    public static function getDefaultUserPlaned($themeid,$page_start=0,$page_size=50)
    {
        $condition="";
        $condition .= " ORDER BY a.created_at DESC  ";
        if ($page_size) {
            $condition .= " LIMIT ".$page_start.",".$page_size;
        }
        $sql = " SELECT distinct a.*,b.nickname,b.username, b.mobile,b.avatar,c.mobile as worker_mobile,c.username as worker_name FROM ".tablename(plan_user_planning)." AS a ".
               " LEFT JOIN ".tablename(t_fans)." AS b ON a.uid=b.uid
               LEFT JOIN ".tablename(t_record)." AS d ON a.storeid=d.themeid and a.uid=d.uid
               LEFT JOIN ".tablename(t_worker)." AS c ON d.workerid=c.uid and d.themeid=c.themeid  
               WHERE a.storeid=$themeid AND b.themeid=$themeid".$condition;

        return db_select($sql);
    }

    //搜索获取指定分店id、用户信息、或者起止时间的预约
    public static function searchPlaned($themeid,$shopid,$search='',$page_start=0,$page_size=50,$start_day='',$end_day='')
    {
        $condition="";
        if ($shopid !='all' ){
            $condition .= " AND a.shopid='$shopid'";
        }
        if ($search){
            $condition .= " AND ( b.mobile LIKE '%".$search."%' OR b.username LIKE '%".$search."%' OR a.offline_username LIKE '%".$search."%' OR a.offline_mobile LIKE '%".$search."%' OR a.uid LIKE '%".$search."%' OR b.nickname LIKE '%".$search."%' )";
        }
        if ($start_day && $end_day){
            $condition .= "  AND ( a.plan_at between '".$start_day."' and '".$end_day."' )";
        }
        $condition .= " ORDER BY a.created_at DESC ";
        if ($page_size) {
            $condition.= "LIMIT ".$page_start.",".$page_size;
        }
        $sql = " SELECT distinct a.*,b.nickname,b.username,b.mobile,b.avatar,c.mobile as worker_mobile,c.username as worker_name FROM ".tablename(plan_user_planning)." AS a ".
               " LEFT JOIN ".tablename(t_fans)." AS b ON a.uid=b.uid and b.themeid=$themeid
               LEFT JOIN ".tablename(t_record)." AS d ON a.storeid=d.themeid and a.uid=d.uid
               LEFT JOIN ".tablename(t_worker)." AS c ON d.workerid=c.uid and d.themeid=c.themeid
               WHERE a.storeid=$themeid ".$condition;
        return db_select($sql);
    }

    //删除该id预约
    public static function delPlan($id)
    {
         return db_delete(plan_user_planning,['id'=>$id]);
    }
    /*以上客户预约表*/




}